/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

package semanticagrotagtagger;

import kea.main.KEAKeyphraseExtractor;
import kea.main.KEAModelBuilder;
import kea.stemmers.*;
import kea.stopwords.*;
import java.util.Hashtable;
import java.io.*;
import java.util.*;
import kea.vocab.Vocabulary;

import properties.LoadProperties;
/**
 *
*/

/**
 *
 * @author Olena Medelyan
 * modified by Rishi Kumar rishik@iitk.ac.in BT-MT CSE IIT Kanpur
 *
 */
public class SemanticAgrotagTagger {

        private KEAModelBuilder km;
    private KEAKeyphraseExtractor ke;

    LoadProperties lp = new LoadProperties();

        private String ROOTPATH = lp.getRootPath();
        private String rootPath;



    private void setOptionsTraining() {

            km = new KEAModelBuilder();

            // A. required arguments (no defaults):

            // 1. Name of the directory -- give the path to your directory with documents and keyphrases
            //    documents should be in txt format with an extention "txt"
            //    keyphrases with the same name as documents, but extension "key"
            //    one keyphrase per line!
            km.setDirName(ROOTPATH + "Agrotagger/SemanticAgrotagTagger/testdocs/en/train");

            // 2. Name of the model -- give the path to where the model is to be stored and its name
            km.setModelName(ROOTPATH + "Agrotagger/SemanticAgrotagTagger/testdocs/en/model");

            // 2B. Name of the file containg old stems in training directory
            //-- give the path to where the file containing old stems is to be stored and its name
            km.setStemsName(ROOTPATH + "Agrotagger/SemanticAgrotagTagger/testdocs/en/oldstems");

            // 3. Name of the vocabulary -- name of the file (without extension) that is stored in VOCABULARIES
            //    or "none" if no Vocabulary is used (free keyphrase extraction).
            //    km.setVocabulary("agrovoc");
            km.setVocabulary("agrotag");

            // 4. Format of the vocabulary in 3. Leave empty if vocabulary = "none", use "skos" or "txt" otherwise.
            //    km.setVocabularyFormat("skos");
            km.setVocabularyFormat("text");

            //    B. optional arguments if you want to change the defaults
            // 5. Encoding of the document
            km.setEncoding("UTF-8");

            // 6. Language of the document -- use "es" for Spanish, "fr" for French
            //    or other languages as specified in your "skos" vocabulary
            km.setDocumentLanguage("en"); // es for Spanish, fr for French

            // 7. Stemmer -- adjust if you use a different language than English or if you want to alterate results
            // (We have obtained better results for Spanish and French with NoStemmer)
            km.setStemmer(new PorterStemmer());

            // 8. Stopwords -- adjust if you use a different language than English!
            km.setStopwords(new StopwordsEnglish());

            // 9. Maximum length of a keyphrase
            km.setMaxPhraseLength(2);

            // 10. Minimum length of a keyphrase
            km.setMinPhraseLength(1);

            // 11. Minumum occurrence of a phrase in the document -- use 2 for long documents!
            km.setMinNumOccur(2);

    }


    private void setOptionsTesting(int m_numPhrases/*String m_testdir*/) {

        ke = new KEAKeyphraseExtractor();

        // A. required arguments (no defaults):

        // 1. Name of the directory -- give the path to your directory with documents
        //    documents should be in txt format with an extention "txt".
        //    Note: keyphrases with the same name as documents, but extension "key"
        //    one keyphrase per line!
                String m_testdir = "testdocs/en/test";
        ke.setDirName(ROOTPATH + "Agrotagger/SemanticAgrotagTagger/testdocs/en/test");

        // 2. Name of the model -- give the path to the model
        ke.setModelName(ROOTPATH + "Agrotagger/SemanticAgrotagTagger/testdocs/en/model");

        // 3. Name of the vocabulary -- name of the file (without extension) that is stored in VOCABULARIES
        //    or "none" if no Vocabulary is used (free keyphrase extraction).
                //        ke.setVocabulary("agrovoc");
                ke.setVocabulary("agrotag");

        // 4. Format of the vocabulary in 3. Leave empty if vocabulary = "none", use "skos" or "txt" otherwise.
                //        ke.setVocabularyFormat("skos");
                ke.setVocabularyFormat("text");

                // B. optional arguments if you want to change the defaults
        // 5. Encoding of the document
        ke.setEncoding("UTF-8");

        // 6. Language of the document -- use "es" for Spanish, "fr" for French
        //    or other languages as specified in your "skos" vocabulary
        ke.setDocumentLanguage("en"); // es for Spanish, fr for French

        // 7. Stemmer -- adjust if you use a different language than English or want to alterate results
        // (We have obtained better results for Spanish and French with NoStemmer)
        ke.setStemmer(new PorterStemmer());

        // 8. Stopwords
        ke.setStopwords(new StopwordsEnglish());

        // 9. Number of Keyphrases to extract
        ke.setNumPhrases(m_numPhrases);

        // 10. Set to true, if you want to compute global dictionaries from the test collection
        ke.setBuildGlobal(false);
    }



    private void createModel() {
       try {
            System.err.println("inside create model");
//            km.openModel();
//            km.openStems();
//            Hashtable newStems = km.identifyNewStems();
//            System.err.println("new stems:  " + newStems);
//            km.incrementalBuildModel(newStems);

              System.out.println("stemmer for training: " + km.getStemmer().getStemmerType());
              System.out.println("stopwords for training: " + km.getStopwords().getStopwordsType());
          km.buildModel(km.collectStems());
//            km.saveStems();
//            km.saveStemsNull();
//            km.openStems();
//            Hashtable newStems = km.identifyNewStems();
//            System.err.println("new stems:  " + newStems);

//            km.openModel();
//            Hashtable h = km.collectStems();
//            System.out.println("hashtable:::: ");
//            System.out.println(h.elements());
              km.saveModel();
//            km.openModel();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void extractKeyphrases() {
        try {

                System.out.println("stemmer for testing: " + ke.getStemmer().getStemmerType());
                System.out.println("stopwords for testing: " + ke.getStopwords().getStopwordsType());
                ke.loadModel();
            //System.out.print("model loaded
                        // collect new stems.
            ke.extractKeyphrases(ke.collectStems());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

   public String getTagsFromText(String filePath, int nid, int numPhrases) throws Exception {

                System.out.println("iam called");
                String s = null;

                String openAccessPath = null;
                String stemFileName = null;
                Process p;


                File file = new File(filePath);
                String rootPath="";
                rootPath = ConfigRootPath.getRootPath();
                openAccessPath = ROOTPATH + "Agrotagger/tagger-with-agropedia/openAccess";
                String fileName;

                fileName = file.getName();

                String commandCopyText = null;
                stemFileName = fileName.substring(0, fileName.length() - 4);

                String commandCopyAttachment = null;
                commandCopyAttachment = "cp " + filePath + " " + openAccessPath + "/" + "attachments" + "/" + nid + "_"+  fileName;

                p = Runtime.getRuntime().exec(commandCopyAttachment);

                String commandCopytext = null;
                String inputAttachmentPath = filePath;

                String outputTextPath = openAccessPath + "/" + "texts" + "/" + nid + "_" + stemFileName + ".txt";

                commandCopytext = "cp " + inputAttachmentPath + " " + outputTextPath;



System.out.println("helllllllllllllllllllll0000000000"+commandCopytext);

                 p = Runtime.getRuntime().exec(commandCopytext);

                String inputTextPath = openAccessPath + "/" + "texts" + "/" + nid + "_" + stemFileName + ".txt";

                String taggerRootTestPath = rootPath + "testdocs/en/test/write.txt";

                commandCopyText = "cp " + inputTextPath + " " + taggerRootTestPath;

                p = Runtime.getRuntime().exec(commandCopyText);


                BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

                BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

                // read the output from the command

                System.out.println("Here is the standard output of the command:\n");
                while ((s = stdInput.readLine()) != null) {
                    System.out.println(s);
                }


                // read any errors from the attempted command

                System.out.println("Here is the standard error of the command (if any):\n");
                while ((s = stdError.readLine()) != null) {
                    System.out.println(s);
                }

                String taggerKeyFileName = rootPath + "testdocs/en/test/write.key";
                File taggerKeyFile = new File(taggerKeyFileName);
                if (taggerKeyFile.exists()) {

                     try {

                                 if (taggerKeyFile.delete())
                                    System.err.println("** Deleted " + taggerKeyFileName + " **");
                                 else
                                     System.err.println("Failed to delete " + taggerKeyFileName);
                          } catch (SecurityException e) {
                                 System.err.println("Unable to delete " + taggerKeyFileName + "("
                                       + e.getMessage() + ")");
                                                         }


                }

                this.setOptionsTesting(numPhrases);
                this.extractKeyphrases();

            try {

               // String fileName;

                //fileName = file.getName();

                //stemFileName = fileName.substring(0, fileName.length() - 4);



                System.out.println(stemFileName);

                String commandCopyKey = null;


                taggerRootTestPath = rootPath + "testdocs/en/test";

                String inputKeyPath = taggerRootTestPath +"/" + "write.key";
                System.out.println("***********************"+inputKeyPath);

                String outputKeyPath = openAccessPath + "/" + "keys" + "/" + nid + "_" + stemFileName + ".key";

                commandCopyKey = "cp " + inputKeyPath + " " + outputKeyPath;

                p = Runtime.getRuntime().exec(commandCopyKey);


                // read the output from the command

                System.out.println("Here is the standard output of the command:\n");
                while ((s = stdInput.readLine()) != null) {
                    System.out.println(s);
                }


                // read any errors from the attempted command

                System.out.println("Here is the standard error of the command (if any):\n");
                while ((s = stdError.readLine()) != null) {
                    System.out.println(s);
                }
                //System.exit(0)
        }
        catch (Exception e) {
            System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
           // System.exit(-1);
        }


        /*
         * return tag-set
         *
         * */

        try {

                            //String fileName;

                            //fileName = file.getName();

                          // String stemFileName = null;

                            BufferedReader input = null;
                            String keyFileName = null;
                            keyFileName = openAccessPath + "/" + "keys" + "/" + nid + "_" + stemFileName + ".key";
                            System.out.println("keyFileName : " + keyFileName);
                            input = new BufferedReader(new FileReader(keyFileName));
                            //System.out.println("keyfile exits or not? : " + new File(keyFileName).exists());
                            String tagSet = "";
                            String text = "";
                            int c=0;
                            while((text=input.readLine())!= null) {
                                //text = text.replace(" ", "_");
                                if (!(text.equalsIgnoreCase("0") || text.equalsIgnoreCase("NULL")))
                                {
                                    tagSet = tagSet.concat(text);
                                    tagSet = tagSet.concat(" | ");
                                    c++;
                                }
                                System.out.println("keys " + c + " ::"+ text);
                            }
                            input.close();
                            tagSet = tagSet.trim();

                            return tagSet.substring(0, tagSet.length()-2);
        }
        catch (Exception e) {
            System.out.println("Exception in reading tags: " + e);
            System.exit(0);
            return "NOT-AVAILABLE";
        }


    }

    public String getTagsFromAttachment(String filePath, int nid, int numPhrases) {
        String s = null;

        String openAccessPath = null;
        openAccessPath = ROOTPATH + "Agrotagger/tagger-with-agropedia/openAccess";

        Process p;
        File file = new File(filePath);
        String fileName;
        fileName = file.getName();

        String rootPath="";
        rootPath = ConfigRootPath.getRootPath();
     //   rootPath=ROOTPATH + "Agrotagger/SemanticTagger/";
        System.out.println("rootPath: " + rootPath);
        System.out.println("Inside RootPath: " + ConfigRootPath.getRootPath());

         copyAttachmentToOpenaccessAttachment(fileName,rootPath,filePath,nid,openAccessPath);
         convertAttachmentInPdf(fileName,openAccessPath,nid);
         copyAttachmentToPdf(fileName,filePath,openAccessPath,nid);
         convertConvertedPdfToText(fileName,filePath,openAccessPath,nid);
         copyConvertedText(fileName,rootPath,openAccessPath,nid);
         checkKeyFile(fileName,rootPath,openAccessPath,nid,numPhrases);

         //extract tags frm key file
         String tags=tagSet(fileName,openAccessPath,nid);
    return tags;

    }
     /*
         copy attachment to openAccess/attachments
         *
         * */
       public void copyAttachmentToOpenaccessAttachment(String fileName,String rootPath,String filePath,int nid,String openAccessPath)
       {
            String s = null;
            Process p;

        try {


                System.out.println(fileName);
                System.out.println(rootPath);

                System.out.println(filePath);

                System.out.println(nid);

                System.out.println(openAccessPath);



                String commandCopyAttachment = null;
                commandCopyAttachment = "cp " + filePath + " " + openAccessPath + "/" + "attachments" + "/" + nid + "_"+  fileName;
                System.out.println("Attachment Copy ==>" + commandCopyAttachment);
        //        commandCopyAttachment = "cp " + filePath  + " " + openAccessPath + "/" + "attachments" + "/" + nid + "_"+  fileName;


                p = Runtime.getRuntime().exec(commandCopyAttachment);

                BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

                BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

                // read the output from the command

                System.out.println("Here is the standard output of the command:\n");
                while ((s = stdInput.readLine()) != null) {
                    System.out.println(s);
                }

                // read any errors from the attempted command

                System.out.println("Here is the standard error of the command (if any):\n");
                while ((s = stdError.readLine()) != null) {
                    System.out.println(s);
                }

                //System.exit(0)
        }
        catch (Exception e) {
            System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
           // System.exit(-1);
        }
       }

        /*
         convert attachment(if not in pdf format) to pdf and save it to openAccess/pdfs
         *
         * */
public void convertAttachmentInPdf(String fileName,String openAccessPath,int nid)
       {

      String s = null;
      Process p;

        try {
                System.out.println("----------------------------------------------\n" +
                        "converting to pdf.......");



                String stemFileName = null;
                if(     fileName.endsWith(".ppt")
                        || fileName.endsWith(".pdf")
                        || fileName.endsWith(".doc")
                        || fileName.endsWith(".xls")
                        || fileName.endsWith(".odt")
                        || fileName.endsWith(".odp")
                        || fileName.endsWith(".ods")
                        || fileName.endsWith(".txt")
                       )
                {
                    stemFileName = fileName.substring(0, fileName.length() - 4);
                }

                if( fileName.endsWith(".pptx")
                       || fileName.endsWith(".docx"))
                {
                   stemFileName = fileName.substring(0, fileName.length() - 5);
                }

                System.out.println(stemFileName);

                String commandStartOpenOfficeService = null;
                commandStartOpenOfficeService = "soffice -headless -accept=\"socket,port=8100;urp;\"";

                String commandConvert2Pdf = null;

                String jodConverterPath = ROOTPATH + "Agrotagger/jodconverter-2.2.2/lib/jodconverter-cli-2.2.2.jar";

                String inputAttachmentPath = openAccessPath + "/" + "attachments" + "/" + nid + "_" + fileName;

                String outputPdfPath = openAccessPath + "/" + "pdfs" + "/" + nid + "_" + stemFileName + ".pdf";

                commandConvert2Pdf = "java -jar " + jodConverterPath + " " + inputAttachmentPath + " " + outputPdfPath;

                System.out.println("command to convert to pdf: " + commandConvert2Pdf);

                if (!fileName.endsWith(".pdf"))
                {
                    System.out.println("command being executed.....");

                    p = Runtime.getRuntime().exec(commandStartOpenOfficeService);

                    p = Runtime.getRuntime().exec(commandConvert2Pdf);


                    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

                    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

                    // read the output from the command

                    System.out.println("Here is the standard output of the command:\n");
                    while ((s = stdInput.readLine()) != null) {
                        System.out.println(s);
                    }

                    // read any errors from the attempted command

                    System.out.println("Here is the standard error of the command (if any):\n");
                    while ((s = stdError.readLine()) != null) {
                        System.out.println(s);
                    }
                }
                    //System.exit(0)
        }
        catch (Exception e) {
            System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
           // System.exit(-1);
        }
}


        /*
         copy attachment(if pdf) to openAccess/pdfs
         *
         * */
       public void copyAttachmentToPdf(String fileName,String filePath,String openAccessPath,int nid)
       {
           String s=null;
           Process p;

        try {


                System.out.println("filename of pdf "+fileName);

                String stemFileName = null;

                if(     fileName.endsWith(".pdf")
                        || fileName.endsWith(".ppt")
                        || fileName.endsWith(".doc")
                        || fileName.endsWith(".xls")
                        || fileName.endsWith(".odt")
                        || fileName.endsWith(".odp")
                        || fileName.endsWith(".ods")
                        || fileName.endsWith(".txt")
                        )
                {

                    stemFileName = fileName.substring(0, fileName.length() - 4);
                     System.out.println("filename of stem  "+stemFileName);
                }
                else

                if( fileName.endsWith(".pptx")
                        || fileName.endsWith(".docx"))
                {
                    stemFileName = fileName.substring(0, fileName.length() - 5);
                }
                else

                if (fileName.endsWith(".pdf") )
                {

                    stemFileName = fileName.substring(0, fileName.length() - 4);
                    System.out.println("in pdf block "+stemFileName);
                }

                else
                {
                    System.out.println("|||||||||||");
                  //  return "|||||||||";
                }





                System.out.println(stemFileName);

                String commandCopyPdf = null;

                String inputAttachmentPath = filePath;

                String outputPdfPath = openAccessPath + "/" + "pdfs" + "/" + nid + "_" + stemFileName + ".pdf";

                commandCopyPdf = "cp " + inputAttachmentPath + " " + outputPdfPath;


                if (fileName.endsWith(".pdf"))
                {
                    System.out.println("Hari Bol: File ends with .pdf");

                    p = Runtime.getRuntime().exec(commandCopyPdf);


                    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

                    BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

                    // read the output from the command

                    System.out.println("Here is the standard output of the command:\n");
                    while ((s = stdInput.readLine()) != null) {
                        System.out.println(s);
                    }


                    // read any errors from the attempted command

                    System.out.println("Here is the standard error of the command (if any):\n");
                    while ((s = stdError.readLine()) != null) {
                        System.out.println(s);
                    }
                }
                    //System.exit(0)
        }
        catch (Exception e) {
            System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
           // System.exit(-1);
        }

}

        /*
         convert converted pdf to text ans save the text in openAcess/texts
         *
         * */
       public void convertConvertedPdfToText(String fileName,String filePath,String openAccessPath,int nid)
       {

           String s=null;
           Process p;

        try {



                String stemFileName = null;

                if(     fileName.endsWith(".ppt")
                        || fileName.endsWith(".pdf")
                        || fileName.endsWith(".doc")
                        || fileName.endsWith(".xls")
                        || fileName.endsWith(".odt")
                        || fileName.endsWith(".odp")
                        || fileName.endsWith(".ods")
                        || fileName.endsWith(".txt")
                        )
                {
                    stemFileName = fileName.substring(0, fileName.length() - 4);
                }

                if( fileName.endsWith(".pptx")
                        || fileName.endsWith(".docx"))
                {
                    stemFileName = fileName.substring(0, fileName.length() - 5);
                }


                System.out.println(stemFileName);

                String commandConvertPdf2Txt = null;

                String inputPdfPath = openAccessPath + "/" + "pdfs" + "/" + nid + "_" + stemFileName + ".pdf";

                String outputTextPath = openAccessPath + "/" + "texts" + "/" + nid + "_" + stemFileName + ".txt";

                String PDFTextParserJarPath = ROOTPATH + "Agrotagger/PDFTextParser/dist/PDFTextParser.jar";

                commandConvertPdf2Txt = "java -jar " + PDFTextParserJarPath + " " +  inputPdfPath + " " + outputTextPath;

                p = Runtime.getRuntime().exec(commandConvertPdf2Txt);

                BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

                BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

                // read the output from the command

                System.out.println("Here is the standard output of the command:\n");
                while ((s = stdInput.readLine()) != null) {
                    System.out.println(s);
                }


                // read any errors from the attempted command

                System.out.println("Here is the standard error of the command (if any):\n");
                while ((s = stdError.readLine()) != null) {
                    System.out.println(s);
                }
                //System.exit(0)
        }
        catch (Exception e) {
                System.out.println("exception happened - here's what I know: ");
                e.printStackTrace();
               // System.exit(-1);
        }
}
        /*
         copy the converted text to tagger-root/test/ directory for extraction of tags
         *
         * */
       public void copyConvertedText(String fileName,String rootPath,String openAccessPath,int nid)
       {
           String s=null;
           Process p;

        try {


                String stemFileName = null;

                if(     fileName.endsWith(".ppt")
                        || fileName.endsWith(".pdf")
                        || fileName.endsWith(".doc")
                        || fileName.endsWith(".xls")
                        || fileName.endsWith(".odt")
                        || fileName.endsWith(".odp")
                        || fileName.endsWith(".ods")
                        || fileName.endsWith(".txt")
                        )
                {
                    stemFileName = fileName.substring(0, fileName.length() - 4);
                }

                if( fileName.endsWith(".pptx")
                        || fileName.endsWith(".docx"))
                {
                    stemFileName = fileName.substring(0, fileName.length() - 5);
                }


                System.out.println(stemFileName);

                String commandCopyText = null;

                String inputTextPath = openAccessPath + "/" + "texts" + "/" + nid + "_" + stemFileName + ".txt";

                String taggerRootTestPath = rootPath + "testdocs/en/test/write.txt";

                commandCopyText = "cp " + inputTextPath + " " + taggerRootTestPath;

                p = Runtime.getRuntime().exec(commandCopyText);


                BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

                BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

                // read the output from the command

                System.out.println("Here is the standard output of the command:\n");
                while ((s = stdInput.readLine()) != null) {
                    System.out.println(s);
                }


                // read any errors from the attempted command

                System.out.println("Here is the standard error of the command (if any):\n");
                while ((s = stdError.readLine()) != null) {
                    System.out.println(s);
                }
                //System.exit(0)
        }
        catch (Exception e) {
            System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
           // System.exit(-1);
        }
}
        /*
         * use the tagger to extract tags from taggerRootTestPath/testdocs/en/test/write.txt
         * and copy write.key to openAccess/keys/nid_filename.key
         *
         * */

        //check if taggerRootTestPath/testdocs/en/test/write.key exits. If yes delete it.
public void checkKeyFile(String fileName,String rootPath,String openAccessPath,int nid,int numPhrases)
{
    String s=null;
    Process p;

        String taggerKeyFileName = rootPath + "testdocs/en/test/write.key";
        File taggerKeyFile = new File(taggerKeyFileName);
        if (taggerKeyFile.exists()) {

            try {

                  if (taggerKeyFile.delete())
                    System.err.println("** Deleted " + taggerKeyFileName + " **");
                  else
                    System.err.println("Failed to delete " + taggerKeyFileName);
                } catch (SecurityException e) {
                  System.err.println("Unable to delete " + taggerKeyFileName + "("
                      + e.getMessage() + ")");
                }


        }
        this.setOptionsTesting(numPhrases);
        this.extractKeyphrases();

        try {



                String stemFileName = null;

                if(     fileName.endsWith(".ppt")
                        || fileName.endsWith(".pdf")
                        || fileName.endsWith(".doc")
                        || fileName.endsWith(".xls")
                        || fileName.endsWith(".odt")
                        || fileName.endsWith(".odp")
                        || fileName.endsWith(".ods")
                        || fileName.endsWith(".txt")
                        )
                {
                    stemFileName = fileName.substring(0, fileName.length() - 4);
                }

                if( fileName.endsWith(".pptx")
                        || fileName.endsWith(".docx"))
                {
                    stemFileName = fileName.substring(0, fileName.length() - 5);
                }


                System.out.println(stemFileName);

                String commandCopyKey = null;


                String taggerRootTestPath = rootPath + "testdocs/en/test";

                String inputKeyPath = taggerRootTestPath + "/" + "write.key";

                String outputKeyPath = openAccessPath + "/" + "keys" + "/" + nid + "_" + stemFileName + ".key";

                commandCopyKey = "cp " + inputKeyPath + " " + outputKeyPath;

                p = Runtime.getRuntime().exec(commandCopyKey);


                BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

                BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

                // read the output from the command

                System.out.println("Here is the standard output of the command:\n");
                while ((s = stdInput.readLine()) != null) {
                    System.out.println(s);
                }


                // read any errors from the attempted command

                System.out.println("Here is the standard error of the command (if any):\n");
                while ((s = stdError.readLine()) != null) {
                    System.out.println(s);
                }
                //System.exit(0)
        }
        catch (Exception e) {
            System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
           // System.exit(-1);
        }
}

        /*
         * return tag-set
         *
         * */
       public String tagSet(String fileName,String openAccessPath,int nid)
       {

        try {



                            String stemFileName = null;

                            if(     fileName.endsWith(".ppt")
                                    || fileName.endsWith(".pdf")
                                    || fileName.endsWith(".doc")
                                    || fileName.endsWith(".xls")
                                    || fileName.endsWith(".odt")
                                    || fileName.endsWith(".odp")
                                    || fileName.endsWith(".ods")
                                    || fileName.endsWith(".txt")
                               )
                            {
                                 stemFileName = fileName.substring(0, fileName.length() - 4);
                            }

                            if( fileName.endsWith(".pptx")
                                || fileName.endsWith(".docx"))
                            {
                                  stemFileName = fileName.substring(0, fileName.length() - 5);
                            }

                            BufferedReader input = null;
                            String keyFileName = null;
                            keyFileName = openAccessPath + "/" + "keys" + "/" + nid + "_" + stemFileName + ".key";
                            System.out.println("keyFileName : " + keyFileName);
                            try{
                            input = new BufferedReader(new FileReader(keyFileName));
                                }
                            catch(Exception e)
                                {
                                    System.out.println("exception "+e.getMessage());
                                }
                            System.out.println("keyfile exits or not? : " + new File(keyFileName).exists());
                            String tagSet = "";
                            String text = "";
                            int c=0;
                            while((text=input.readLine())!= null) {
                                //text = text.replace(" ", "_");
                                if (!(text.equalsIgnoreCase("0") || text.equalsIgnoreCase("NULL")))
                                {
                                    tagSet = tagSet.concat(text);
                                    tagSet = tagSet.concat(" | ");
                                    c++;
                                }
                                System.out.println("keys " + c + " ::"+ text);
                            }
                            input.close();
                            tagSet = tagSet.trim();

                            return tagSet.substring(0, tagSet.length()-2);
        }
        catch (Exception e) {
            System.out.println("Exception in reading tags: " + e);
            return "NOT-AVAILABLE";
        }


       }



    /**
     * Example file on how to use KEA from your Java code
     * with sample documents included in this package.
     *
     * @param args
     */
    public static void main(String[] args) {
                ConfigRootPath.setRootPath(args[0]);
                System.out.println("root path : " + ConfigRootPath.getRootPath());

        SemanticAgrotagTagger test = new SemanticAgrotagTagger();
                String tagSet="";
                //tagSet = test.getTagsFromAttachment("/root/Desktop/report.pdf", 10, 10);
                System.out.println("TAG SET: " + tagSet);




        // to create a model from manually indexed documents,
        System.err.println("Creating the model... ");
        test.setOptionsTraining();

//                test.createModel();
//
        // to extract keyphrases from new documents,
        System.err.println("Extracting keyphrases from test documents... ");

        String m_testdir = ConfigRootPath.getRootPath() + "testdocs/en/test";
                System.out.println("test directory: " + m_testdir);
        test.setOptionsTesting(10);
////                Vector v = new Vector();
////                String str = "What is this consciousness? This consciousness is \"I am.\" Then what am I? In contaminated consciousness \"I am\" means \"I am the lord of all I survey. I am the enjoyer.\" The world revolves because every living being thinks that he is the lord and creator of the material world. Material consciousness has two psychic divisions. One is that I am the creator, and the other is that I am the enjoyer. But actually the Supreme Lord is both the creator and the enjoyer, and the living entity, being part and parcel of the Supreme Lord, is neither the creator nor the enjoyer, but a cooperator. He is the created and the enjoyed. For instance, a part of a machine cooperates with the whole machine; a part of the body cooperates with the whole body. The hands, legs, eyes, and so on are all parts of the body, but they are not actually the enjoyers. The stomach is the enjoyer. The legs move, the hands supply food, the teeth chew, and all parts of the body are engaged in satisfying the stomach because the stomach is the principal factor that nourishes the body's organization. Therefore everything is given to the stomach. One nourishes the tree by watering its root, and one nourishes the body by feeding the stomach, for if the body is to be kept in a healthy state, then the parts of the body must cooperate to feed the stomach. Similarly, the Supreme Lord is the enjoyer and the creator, and we, as subordinate living beings, are meant to cooperate to satisfy Him. This cooperation will actually help us, just as food taken by the stomach will help all other parts of the body. If the fingers of the hand think that they should take the food themselves instead of giving it to the stomach, then they will be frustrated. The central figure of creation and of enjoyment is the Supreme Lord, and the living entities are cooperators. By cooperation they enjoy. The relation is also like that of the master and the servant. If the master is fully satisfied, then the servant is satisfied. Similarly, the Supreme Lord should be satisfied, although the tendency to become the creator and the tendency to enjoy the material world are there also in the living entities because these tendencies are there in the Supreme Lord who has created the manifested cosmic world.";
////                try {
////                    v = test.getTags(str, 10);
////                }
////                catch (Exception e) {
////                    System.out.println(e);
////                }
////                Vocabulary voc = new Vocabulary("agrovoc", "skos", "en");
////                System.out.println(voc.containsEntry(""));
////                str = voc.pseudoPhrase("I am Rishi Kumar a very arrogant perosn");
////                System.out.println(str);
//
//
//
////                int count1 = 0;
////                while(!v.isEmpty()) {
////                    System.out.println("hariiiiiiiii bol " + count1 + " " + v.elementAt(count1));
////                    count1++;
////                }
//
        test.extractKeyphrases();

        System.err.println("Results in this directory: " +ConfigRootPath.getRootPath() +  m_testdir);
        System.err.println("and compare them to " + ConfigRootPath.getRootPath() + m_testdir + "/manual_keyphrases/.");


    }

}
